<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */
namespace app\api\model\dao;

use think\Model;
use think\Db;

class Signin extends Model
{
    protected $table = 'signin';

    protected $pk = 'id';

    /**
     * 签到
     *
     */
    public function addSignin($arrInput)
    {
        unset($arrInput[$this->pk]);

        $this->data($arrInput);
        $this->save();
        return $this->data[$this->pk];
    }

    /**
     * 判断某人某天是否签到
     *
     */
    public function isSignin($userid, $date = null)
    {
        if (empty($userid)) {
            return false;
        }
        if (empty($date)) {
            $date = date('Ymd');
        }
        $conds = [
            'userid' => ['=', $userid],
            'date' => ['=', $date],
        ];
        return Db::table($this->table)->where($conds)->count();
    }

    /**
     * 获取某人某段时间签到列表
     *
     */
    public function getSigninByUserid($userid, $sDate = null, $eDate = null)
    {
        if (empty($userid)) {
            return false;
        }
        if (empty($sDate)) {
            $sDate = date('Ym01');
        }
        if (empty($eDate)) {
            $eDate = date('Ymd');
        }
        $conds = [
            'userid' => ['=', $userid],
            'date' => ['>=', $sDate, '<=', $eDate],
        ];
        return Db::table($this->table)->where($conds)->order(' date asc ')->select();
    }

    /**
     * 获取签到列表
     *
     */
    public function getSignin($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

}
